home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Small Eiffel 0.4.8 / lib_std / array.e < prev    next >
Encoding:
Text File  |  1997-04-13  |  6.2 KB  |  302 lines  |  [TEXT/ttxt]

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. class ARRAY[E]
  5.    -- 
  6.    -- General purpose arrays.
  7.    --
  8.  
  9. inherit ARRAYED_COLLECTION[E];
  10.  
  11. creation make, with_capacity, from_collection
  12.  
  13. feature
  14.    
  15.    lower: INTEGER;     
  16.      -- Lower index bound.
  17.    
  18. feature -- Creation and Modification :
  19.    
  20.    make(minindex, maxindex: INTEGER) is
  21.      -- Make array with range [minindex .. maxindex]. 
  22.      -- When maxindex = minindex - 1, the array is empty.
  23.       require
  24.      minindex -1 <= maxindex
  25.       local
  26.      needed: INTEGER;
  27.       do
  28.      lower := minindex;
  29.      upper := maxindex;
  30.      needed := maxindex - minindex + 1;
  31.      if needed /= 0 then
  32.         if capacity < needed then
  33.            if capacity = 0 then
  34.           storage.malloc(needed);
  35.            else
  36.           storage.realloc(storage,needed);
  37.            end;
  38.            capacity := needed;
  39.         end;
  40.         clear_all;
  41.      end;
  42.       ensure
  43.      lower = minindex;
  44.      upper = maxindex;
  45.      all_cleared
  46.       end;
  47.  
  48.    with_capacity(needed_capacity, low: INTEGER) is
  49.      -- Create an empty array with `capacity' initialized
  50.      -- at least to `needed_capacity' and `lower' set to `low'.
  51.       require
  52.      needed_capacity >= 0
  53.       do
  54.      if capacity < needed_capacity then
  55.         if capacity = 0 then
  56.            storage.malloc(needed_capacity);
  57.         else
  58.            storage.realloc(storage,needed_capacity);
  59.         end;
  60.         capacity := needed_capacity;
  61.      end;
  62.      lower := low;
  63.      upper := low - 1;
  64.       ensure
  65.      empty;
  66.      needed_capacity <= capacity;
  67.      lower = low
  68.       end;
  69.  
  70. feature -- Modification :
  71.    
  72.    resize(minindex, maxindex: INTEGER) is
  73.      -- Resize the array. No elements will be lost in the 
  74.      -- intersection of [old lower .. old upper] and 
  75.      -- [minindex .. maxindex].
  76.      -- New positions are initialized with appropriate 
  77.      -- default values.
  78.       require
  79.      minindex -1 <= maxindex
  80.       local
  81.      other: like Current;
  82.      i, up: INTEGER;
  83.      mem: MEMORY;
  84.       do
  85.      from
  86.         !!other.make(minindex,maxindex);
  87.         i := lower.max(other.lower);
  88.         up := upper.min(other.upper);
  89.      until
  90.         i > up
  91.      loop
  92.         other.put(item(i),i);
  93.         i := i + 1;
  94.      end;
  95.      if storage.is_not_void then
  96.         storage.free;
  97.      end;
  98.      standard_copy(other);
  99.      mem.free(other.to_pointer);
  100.       end;
  101.  
  102. feature 
  103.  
  104.    sub_array(low, up: INTEGER): like Current is
  105.       local
  106.      i: INTEGER;
  107.       do
  108.      from
  109.         !!Result.with_capacity(up - low + 1,low);
  110.         Result.set_upper(up);
  111.         i := low;
  112.      until
  113.         i > up
  114.      loop
  115.         Result.put(item(i),i);
  116.         i := i + 1;
  117.      end;
  118.       ensure then
  119.      Result.lower = low;
  120.       end;
  121.  
  122. feature -- Implementation of deferred :
  123.  
  124.    count: INTEGER is
  125.       do
  126.      Result := upper - lower + 1;
  127.       end;
  128.  
  129.    item, infix "@" (index: INTEGER): E is
  130.       do
  131.      Result := storage.item(index - lower);
  132.       end;
  133.    
  134.    put(element: like item; index: INTEGER) is
  135.       do
  136.      storage.put(element,index - lower);
  137.       end;
  138.  
  139.    force(element: like item; index: INTEGER) is
  140.      -- Put `element' at position `index'. Collection is
  141.      -- resized if `index' is not inside current bounds.
  142.      -- New bounds are initialized with default values.
  143.       require else
  144.      true
  145.       do
  146.      if upper < index then
  147.         resize(lower,index);
  148.      elseif index < lower then
  149.         resize(index,upper);
  150.      end;
  151.      put(element,index);
  152.       end;
  153.  
  154.    copy(other: like Current) is
  155.      -- Copy `other' onto Current.
  156.       local
  157.      needed_capacity: INTEGER;
  158.       do
  159.      lower := other.lower;
  160.      upper := other.upper;
  161.      needed_capacity := upper - lower + 1;
  162.      if capacity < needed_capacity then
  163.         if capacity = 0 then
  164.            capacity := needed_capacity;
  165.            storage.malloc(capacity);
  166.         else
  167.            capacity := needed_capacity;
  168.            storage.realloc(storage,capacity);
  169.         end;
  170.      end;
  171.      if needed_capacity > 0 then
  172.         storage.copy_from(other.storage,needed_capacity - 1);
  173.      end;
  174.       end;
  175.    
  176.    set_all_with(v: like item) is
  177.       do
  178.      storage.set_all_with(v,upper - lower);
  179.       end;
  180.  
  181.    remove_first is
  182.       do
  183.      storage.remove_first(upper - lower);
  184.      lower := lower + 1;
  185.       ensure then
  186.      upper = old upper;
  187.       end;
  188.    
  189.    remove(index: INTEGER) is
  190.       do
  191.      storage.remove(index - lower,upper - lower);
  192.      upper := upper - 1;
  193.       end;
  194.    
  195.    clear is
  196.       do
  197.      upper := lower - 1;
  198.       ensure then
  199.      capacity = old capacity
  200.       end;
  201.  
  202.    add_first(element: like item) is
  203.       do
  204.      if upper < lower then
  205.         add_last(element);
  206.      else
  207.         add_last(element);
  208.         move(lower,upper - 1,1);
  209.         put(element,lower);
  210.      end;
  211.       end;
  212.  
  213.    add_last(element: like item) is
  214.       do
  215.      if capacity < count + 1 then
  216.         if capacity = 0 then
  217.            capacity := 16;
  218.            storage.malloc(capacity);
  219.         else
  220.            capacity := 2 * capacity;
  221.            storage.realloc(storage,capacity);
  222.         end;
  223.      end;
  224.      upper := upper + 1;
  225.      put(element,upper);
  226.       end;
  227.  
  228.    from_collection(model: COLLECTION[like item]) is
  229.       local
  230.      i, up: INTEGER;
  231.       do
  232.      from
  233.         with_capacity(model.count,model.lower);
  234.         i := model.lower;
  235.         up := model.upper;
  236.         upper := up;
  237.      until
  238.         i > up
  239.      loop
  240.         put(model.item(i),i);
  241.         i := i + 1;
  242.      end;
  243.       ensure then
  244.      lower = model.lower;
  245.      upper = model.upper
  246.       end;
  247.  
  248.    all_cleared: BOOLEAN is
  249.       do
  250.      Result := storage.all_cleared(upper - lower);
  251.       end;
  252.  
  253.    nb_occurrences(element: like item): INTEGER is
  254.       do
  255.      Result := storage.nb_occurrences(element,upper - lower);
  256.       end;
  257.       
  258.    fast_nb_occurrences(element: like item): INTEGER is
  259.       do
  260.      Result := storage.fast_nb_occurrences(element,upper - lower);
  261.       end;
  262.       
  263.    index_of(element: like item): INTEGER is
  264.       do
  265.      Result := lower + storage.index_of(element,upper - lower);
  266.       end;
  267.  
  268.    fast_index_of(element: like item): INTEGER is
  269.       do
  270.      Result := lower + storage.fast_index_of(element,upper - lower);
  271.       end;
  272.  
  273.    is_equal(other: like Current): BOOLEAN is
  274.       do
  275.      if Current = other then
  276.         Result := true;
  277.      elseif lower = other.lower and then upper = other.upper then
  278.         if count = 0 then
  279.            Result := true;
  280.         else
  281.            Result := storage.memcmp(other.storage,count);
  282.         end;
  283.      end;
  284.       end;
  285.  
  286.    slice(low, up: INTEGER): like Current is
  287.       local
  288.      i: INTEGER;
  289.       do
  290.      from
  291.         i := low;
  292.         !!Result.with_capacity(up - low + 1,lower);
  293.      until
  294.         i > up
  295.      loop
  296.         Result.add_last(item(i));
  297.         i := i + 1;
  298.      end;
  299.       end;
  300.      
  301. end -- ARRAY[E]
  302.